﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace TFSCodeReviewHelper
{
    /// <summary>
    /// Interaction logic for ChangesetParticiationControl.xaml
    /// </summary>
    public partial class ChangesetParticiationControl : UserControl, INotifyPropertyChanged 
    {
        protected int FirstIndex;
        protected int LastIndex;

        public event ChangeSelectedEventHandler ChangeSelected;

        public ChangesetParticiationControl()
        {
            InitializeComponent();

            FirstIndex = -1;
            LastIndex = -1;

            DataContext = ChangesForControl;
            
        }

        public void SetAssetChangeList(List<AssetChange> changes)
        {
            FirstIndex = -1;
            LastIndex = -1;
            //_MainStackPanel.Children.Clear();
            //foreach (AssetChange change in changes)
            //{
            //    ChangesetControl control = new ChangesetControl(change);
            //    control.Selected += new ChangeSelectedEventHandler(OnChangeSelected);
            //    _MainStackPanel.Children.Add(control);
            //}
            //((ChangesetControl)_MainStackPanel.Children[0]).MakeFirstEntry();
            Changes = changes;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            if (null != PropertyChanged)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }


        public static readonly DependencyProperty ChangesProperty = DependencyProperty.Register(
                                                            "Changes",
                                                            typeof(List<AssetChange>),
                                                            typeof(ChangesetParticiationControl), new System.Windows.PropertyMetadata(new System.Windows.PropertyChangedCallback(ChangeListChanged)));

        static void ChangeListChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((ChangesetParticiationControl)sender).SetAssetChangeList((List<AssetChange>)e.NewValue);
        }

        private List<AssetChange> _Changes;

        public List<AssetChange> Changes
        {
            get { return (List<AssetChange>)GetValue(ChangesProperty); }
            set
            {
                _Changes = value;
                SetValue(ChangesProperty, value);
                OnPropertyChanged("Changes");
            }
        }


        public List<AssetChange> ChangesForControl
        {
            get { return _Changes; }
            set { _Changes = value; }
        }
        void OnChangeSelected(object sender, ChangeSelectedArgs e)
        {
            //ChangesetControl control = (ChangesetControl)sender;
            
            //int controlIndex = _MainStackPanel.Children.IndexOf(control);

            //if (FirstIndex == -1 && LastIndex == -1)
            //{
            //    FirstIndex = controlIndex;
            //    control.Change.DifferenceParticiaption = DifferenceParticiaption.First;
            //}
            //else if (controlIndex < FirstIndex)
            //{
            //    if (LastIndex != -1)
            //        ((ChangesetControl)_MainStackPanel.Children[LastIndex]).Change.DifferenceParticiaption = DifferenceParticiaption.None;

            //    ((ChangesetControl)_MainStackPanel.Children[FirstIndex]).Change.DifferenceParticiaption = DifferenceParticiaption.Last;
            //    LastIndex = FirstIndex;

            //    FirstIndex = controlIndex;
            //    control.Change.DifferenceParticiaption = DifferenceParticiaption.First;
            //}
            //else
            //{
            //    if (LastIndex != -1)
            //        ((ChangesetControl)_MainStackPanel.Children[LastIndex]).Change.DifferenceParticiaption = DifferenceParticiaption.None;
            //    LastIndex = controlIndex;
            //    control.Change.DifferenceParticiaption = DifferenceParticiaption.Last;
            //}
            
            //if (ChangeSelected != null)
            //{
            //    if (FirstIndex!= -1)
            //        ChangeSelected(this, new ChangeSelectedArgs(((ChangesetControl)_MainStackPanel.Children[FirstIndex]).Change));

            //    if (LastIndex != -1)
            //        ChangeSelected(this, new ChangeSelectedArgs(((ChangesetControl)_MainStackPanel.Children[LastIndex]).Change));
            //}
        }

        public void Reset()
        {
            //if (FirstIndex != -1)
            //    ((ChangesetControl)_MainStackPanel.Children[FirstIndex]).Change.DifferenceParticiaption = DifferenceParticiaption.None;

            //if (LastIndex != -1)
            //    ((ChangesetControl)_MainStackPanel.Children[LastIndex]).Change.DifferenceParticiaption = DifferenceParticiaption.None;
        }
    }





    [ValueConversion(typeof(AssetChange), typeof(ChangesetControl))]
    public class ChangeToControlConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            AssetChange change = (AssetChange)value;
            return new ChangesetControl(change);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((ChangesetControl)value).Change;
        }

    }
}
